<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="资源名称" prop="resourceName">
        <el-input
          v-model="dataForm.resourceName"
          placeholder="资源名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="资源编号" prop="resourceCode">
        <el-input
          v-model="dataForm.resourceCode"
          placeholder="资源编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="资源类型" prop="resourceType">
        <!-- <el-input v-model="dataForm.userPrivilege" placeholder="用户权限"></el-input> -->
        <el-radio-group v-model="dataForm.resourceType">
          <el-radio-button
            v-model="dataForm.resourceType"
            label="word文档"
          ></el-radio-button>
          <el-radio-button
            v-model="dataForm.resourceType"
            label="pdf文档"
          ></el-radio-button>
          <el-radio-button
            v-model="dataForm.resourceType"
            label="学习视频"
          ></el-radio-button>
          <el-radio-button
            v-model="dataForm.resourceType"
            label="压缩文件"
          ></el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="资源地址" prop="resourceAddress">
        <el-input
          v-model="dataForm.resourceAddress"
          placeholder="资源地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="附件" prop="attachmentName">
        <el-upload
          class="upload-demo"
          action="http://localhost:88/api/course/common/upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-success="handleAvatarSuccess"
          multiple
          :limit="3"
          :on-exceed="handleExceed"
          :file-list="fileList"
          v-model="dataForm.attachmentName"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="资源到期时间" prop="resourceDate">
        <!-- <el-input v-model="dataForm.lastLoginTime" placeholder="操作时间"></el-input> -->
        <div class="block">
          <el-date-picker
            v-model="dataForm.resourceDate"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        gmtCreate: "",
        createBy: "",
        gmtModified: "",
        lastModifiedBy: "",
        isDeleted: "",
        sortNo: "",
        version: "",
        resourceName: "",
        resourceCode: "",
        resourceType: "",
        resourceAddress: "",
        resourceDate: "",
        attachmentName:""
      },
      fileList: [],
      dataRule: {
        gmtCreate: [
          { required: true, message: "记录创建时间不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "记录创建者不能为空", trigger: "blur" },
        ],
        gmtModified: [
          { required: true, message: "记录更新时间不能为空", trigger: "blur" },
        ],
        lastModifiedBy: [
          {
            required: true,
            message: "记录最后更新人不能为空",
            trigger: "blur",
          },
        ],
        isDeleted: [
          {
            required: true,
            message: "是否删除(1删除,0未删除)不能为空",
            trigger: "blur",
          },
        ],
        sortNo: [{ required: true, message: "序号不能为空", trigger: "blur" }],
        version: [{ required: true, message: "版本不能为空", trigger: "blur" }],
        resourceName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" },
        ],
        resourceCode: [
          { required: true, message: "资源编号不能为空", trigger: "blur" },
        ],
        resourceType: [
          {
            required: true,
            message: "资源类型(0_教材,1_视频)不能为空",
            trigger: "blur",
          },
        ],
        resourceDate: [
          { required: true, message: "资源到期时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/course/tresourceinformation/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.gmtCreate = data.tResourceInformation.gmtCreate;
              this.dataForm.createBy = data.tResourceInformation.createBy;
              this.dataForm.gmtModified = data.tResourceInformation.gmtModified;
              this.dataForm.lastModifiedBy =
                data.tResourceInformation.lastModifiedBy;
              this.dataForm.isDeleted = data.tResourceInformation.isDeleted;
              this.dataForm.sortNo = data.tResourceInformation.sortNo;
              this.dataForm.version = data.tResourceInformation.version;
              this.dataForm.resourceName =
                data.tResourceInformation.resourceName;
              this.dataForm.resourceCode =
                data.tResourceInformation.resourceCode;
              this.dataForm.resourceType =
                data.tResourceInformation.resourceType;
              this.dataForm.resourceAddress =
                data.tResourceInformation.resourceAddress;
              this.dataForm.resourceDate =
                data.tResourceInformation.resourceDate;
                this.dataForm.attachmentName=data.tResourceInformation.attachmentName
            }
          });
        }
      });
    },
    handlePreview(){},
    handleRemove(){},
    beforeRemove(){},
    handleExceed(){},
    handleAvatarSuccess (response, file, fileList) {
              this.dataForm.attachmentName =response.data
              console.log(this.dataForm.attachmentName);
          },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/course/tresourceinformation/${
                !this.dataForm.id ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              gmtCreate: this.dataForm.gmtCreate,
              createBy: this.dataForm.createBy,
              gmtModified: this.dataForm.gmtModified,
              lastModifiedBy: this.dataForm.lastModifiedBy,
              isDeleted: this.dataForm.isDeleted,
              sortNo: this.dataForm.sortNo,
              version: this.dataForm.version,
              resourceName: this.dataForm.resourceName,
              resourceCode: this.dataForm.resourceCode,
              resourceType: this.dataForm.resourceType,
              resourceAddress: this.dataForm.resourceAddress,
              resourceDate: this.dataForm.resourceDate,
              attachmentName: this.dataForm.attachmentName
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
